package com.beardedcoder.lowPower.activities;

import com.beardedcoder.lowPower.R;
import com.beardedcoder.lowPower.app.AppSettings;

import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemClock;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;

public class PrefLaunch extends PreferenceActivity{
	public static String KEY_PREF_SCREEN_OFF = "prefscreenoff";
	Preference offScreenButton;
	private boolean variablesSet = false;
	private Window windowOfActivity;
	private WindowManager.LayoutParams layoutParamaters;
//	private ActivityManager _AM = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.activity_settings);
		
		// attach listeners
		offScreenButton = (Preference) findPreference(PrefLaunch.KEY_PREF_SCREEN_OFF);
		offScreenButton.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference preference) {
				// boolean returnValue = false;
				try {
					setScreenBrightness(0f);
					//returnValue = true;
					return true;
				} catch (Exception e) {
					AppSettings.log(this.getClass(), "onCreate", e.toString());
				}
				//return returnValue;
				return false;
			}
		});
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		if (variablesSet == false) initialiseVariables();
		setScreenBrightness(0.33f);
	}
	
	private void initialiseVariables() {
		AppSettings.log(this.getClass(), "initialiseVariables",  "Initialising local variables - this should only occur once.");
		windowOfActivity = getWindow();
		layoutParamaters = windowOfActivity.getAttributes();
		variablesSet = true;
	}
	


	private void setScreenBrightness(float f)
	{
		AppSettings.log(this.getClass(), "setScreenBrightness", "Brightness setting attempting to set to: " + f);
		
		if (f == 0)
		{
			//flagAlter(_layoutParamaters, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, "not touchable", true);
			//flagAlter(_layoutParamaters, WindowManager.LayoutParams.FLAG_FULLSCREEN, "fullscreen", true);
		} else
		{
			//flagAlter(_layoutParamaters, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, "not touchable", false);
			//flagAlter(_layoutParamaters, WindowManager.LayoutParams.FLAG_FULLSCREEN, "fullscreen", true);
		}
		
		layoutParamaters.screenBrightness = f;
		windowOfActivity.setAttributes(layoutParamaters);
	}
	
	@SuppressWarnings("unused")
	private void flagAlter(LayoutParams lp, int flag, String flagDesc, boolean bAddFlag)
	{
		boolean bFlagPresent = isFlagPresent(lp.flags, flag);
		AppSettings.log(this.getClass(), "flagAlter", "v1: " + lp.flags + " v2: " + flag + " flagPresent: " + bFlagPresent + " flagName: " + flagDesc);
		
		if (bAddFlag != bFlagPresent)
		{
			// something is required to make them match
			AppSettings.log(this.getClass(), "flagAlter", "Alteration of flag required: " + flagDesc);
			if (bAddFlag == true) lp.flags = lp.flags | flag;
			else if (bAddFlag == false) lp.flags = lp.flags ^ flag;
		}
	}
	
	private boolean isFlagPresent(int i, int f) {return (i & f) != 0;}
	
	@SuppressWarnings("unused")
	private void setPowerOff(long miliSecondsToSleep)
	{
		PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
		long t = SystemClock.elapsedRealtime() + miliSecondsToSleep;
		pm.goToSleep(t);
		
	}
}
